package com.jingdianjichi.oss.util;

import com.jingdianjichi.oss.entity.FileInfo;
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class MinioUtil {

    @Resource
    private MinioClient minioClient;

    @Value("${minio.url}")
    private String url;
    /**
     * 创建桶
     */
    public void createBuket(String bucketName)throws  Exception{
        boolean exists = minioClient.bucketExists(BucketExistsArgs
                .builder()
                .bucket(bucketName).build());
        if(!exists){
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    }

    /**
     * 上传文件
     */
    public void uploadFile(InputStream inputStream, String bucketName, Long size,String fileName)throws  Exception{
        minioClient.putObject(PutObjectArgs
                .builder()
                .bucket(bucketName)
                .object(fileName).stream(inputStream, size,Integer.MAX_VALUE)
                .build());
    }


    /**
     * 获取所有桶
     * @return
     * @throws Exception
     */
    public List<String> getAllBuckets() throws Exception{
        List<Bucket> buckets = minioClient.listBuckets();
        return buckets.stream().map(Bucket::name).collect(Collectors.toList());
    }

    /**
     * 获得当前桶下的 文件
     * @param buket 名称
     * @return 文件
     * @throws Exception 异常
     */

    public List<FileInfo> getAllFiles(String buket) throws Exception{
        Iterable<Result<Item>> results = minioClient
                .listObjects(ListObjectsArgs.builder().bucket(buket).build());
        List<FileInfo> fileInfos = new ArrayList<>();
        for (Result<Item> result : results) {
            FileInfo fileInfo = new FileInfo();
            Item item = result.get();

            fileInfo.setFileName(item.objectName());
            fileInfo.setDirectoryFlag(item.isDir());
            fileInfo.setEtag(item.etag());
            fileInfos.add(fileInfo);
        }
        return fileInfos;
    }

    /**
     * 下载文件
     * @param bucketName 桶
     * @param objectName 文件名
     * @return  文件
     * @throws Exception 错误
     */
    public InputStream downloadFile(String bucketName, String objectName) throws  Exception{
        return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
    }


    /**
     * 删除文件
     * @param bucketName 桶
     */
    public void deleteBucket(String bucketName) throws Exception{
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    }
    /**
     * 删除文件
     * @param bucketName 桶
     * @param objectName 文件
     */
    public void deleteFile(String bucketName, String objectName) throws Exception{
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
    }

    public String getFileUrl(String bucketName, String objectName) throws Exception{
        return url+"/"+bucketName+"/"+objectName;
        /*return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs
                .builder().method(Method.GET).bucket(bucketName).object(objectName).build());*/
    }

}
